#include "GBMors.h"
#include "reg52.h" 
#define LONGPRESSTIME 2000 //长按计数
#define FIXCOUNT 20 //消抖计数
sbit k1 = P3^1;
sbit k2 = P3^0;
sbit k3 = P3^2;
sbit k4 = P3^3;
sbit led0 = P2^0;
sbit led1 = P2^1;
sbit led2 = P2^2;
sbit led3 = P2^3;
sbit led4 = P2^4;
sbit led5 = P2^5;

unsigned long count = 0; //消抖计数
unsigned long pressCount = 0;//长按计数
unsigned char isDownBefore = 0;//是否按下过
enum KEYSTATE{KEYNORMAL,KEYDOWN,KEYUP,KEYLONGPRESS,KEYLONGPRESSUP};
static enum KEYSTATE key1State;
void changeKey1(){
	if(k1 == 0){
		//按键被按下
		if(count >= 20 && k1 == 0){
			isDownBefore = 1;
			//认为按键已经被按下
			if(pressCount == 0){
				key1State = KEYDOWN;
			}
			
			if(pressCount > 2000){
				//进入长按状态
				key1State = KEYLONGPRESS;
			}
			pressCount ++;
			
		}
		if(count >= 20){
			count = 20;
		}else{
			count ++;
		}
	}else{
		
		if(count <= 0 && k1 == 1){
			if(isDownBefore){
				if(pressCount >= 2000){
					//长按释放
					//led0 = ~led0;
				}else{
					//短按释放
					//led1 = ~led1;
					key1State = KEYUP;
				}
				isDownBefore = 0;
			}
			//认为按键已经被抬起
			
			pressCount = 0 ;
			count = 0;	
			
		}
		count --;
	}
	

	
}

unsigned char readKey1(){
	if(key1State == KEYDOWN){
		return 1;
	}else if (key1State == KEYUP){

		return 2;
	}else if (key1State == KEYLONGPRESS){
	
		return 3;
	}else{
		return 99;
	}
	
	
		
}
